using System;
using System.Collections.Generic;
using Mall.Api.Entity;
using Microsoft.AspNetCore.Mvc;
using System.Linq;
using Mall.Api.ParamModel;
using Newtonsoft.Json;
using Mall.Api.Repository;
using Mall.Api.Utils;
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Authorization;
using Mall.Api.Filters;
using Microsoft.AspNetCore.Authentication;

namespace Mall.Api.Controllers
{
    /// <summary>
    /// 生成用户订单
    /// </summary>


    // [Authorize]

    [ApiController]
    [Route("userOrder")]
    public class UserOrderController : ControllerBase
    {
        private IRepository<UserOrder> _userOrder;
        private IRepository<Users> _users;
        private IRepository<Payment> _payment;
        private IRepository<OrderStatu> _orderStatu;

        private readonly IConfiguration _configuration;


        public UserOrderController(IConfiguration configuration, IRepository<UserOrder> userOrder,
         IRepository<Users> users, IRepository<Payment> payment, IRepository<OrderStatu> orderStatu)
        {
            _configuration = configuration;
            _userOrder = userOrder;
            _payment = payment;
            _orderStatu = orderStatu;
            _users = users;
        }


        /// <summary>
        /// 生成订单
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public string Post(NewUserOrder model)
        {
            // 存放订单号
            

            Random rd = new Random();//用于生成随机数
            string DateStr = DateTime.Now.ToString("yyyyMMddHHmmssMM");//日期
            string str = DateStr + rd.Next(10000).ToString().PadLeft(4, '0');//带日期的随机数
          
            var orderCode = str;
            // var usersId = model.UsersId;
            var paymentId = model.PaymentId;
            var orderStatuId = model.OrderStatuId;
            var number = model.Number;
            
            var total = model.Total;

            var claims = base.HttpContext.AuthenticateAsync().Result
                            .Principal.Claims.FirstOrDefault().Subject.Claims;

                var usersId = claims.FirstOrDefault(c => c.Type == "id")?.Value;
            // var rolesid = model.RolesId;



            // var us = _userOrder.Table.Where(r => r.Id == 1).FirstOrDefault();

            var userOrder = new UserOrder
            {
                Users =_users.Table.Single(u => u.Id==int.Parse(usersId)),
                Payment = _payment.Table.Single(p =>p.Id==paymentId),
                OrderStatu = _orderStatu.Table.Single(o =>o.Id ==orderStatuId),
                Number =number,
                OrderCode = orderCode,
                Total =total
                


            };

            _userOrder.Insert(userOrder);


            var res = new
            {
                Code = 200,
                Data = new
                {
                    UserOrder = userOrder
                },
                Msg = "生成订单成功"
            };
            return JsonHelper.Serialize(res);
        }




    }

}